package br.ucb.cesbdao.model.impl;

import java.sql.SQLException;
import java.util.ArrayList;

import lombok.Getter;
import lombok.Setter;

import org.apache.log4j.Logger;

import br.ucb.cesbdao.beans.Acesso;
import br.ucb.cesbdao.model.AcessoModel;
import br.ucb.cesbdao.persistencia.dao.AcessoDAO;
import br.ucb.cesbdao.persistencia.factory.FactoryConnection;
import br.ucb.cesbdao.persistencia.factory.FactoryDAO;
import br.ucb.cesbdao.servicos.EncriptaSenha;
import br.ucb.cesbdao.servicos.Servicos;

public class AcessoModelImpl implements AcessoModel{

	private Logger logger = Servicos.carregaLog(AcessoModelImpl.class);
	@Getter @Setter
	private AcessoDAO dao; 
	
	public AcessoModelImpl() {
	}
	
	@Override
	public int addAcesso(Acesso acesso) {
		try{
			setDao(FactoryDAO.getDaoAcesso(FactoryConnection.getInstance().criaConexao()));
			acesso.setSenha(EncriptaSenha.encripta(acesso.getSenha()));
			return getDao().addAcesso(acesso);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}finally{
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return 0;
	}

	@Override
	public int deletaAcesso(String cpf) {
		try{
			setDao(FactoryDAO.getDaoAcesso(FactoryConnection.getInstance().criaConexao()));
			return getDao().deletaAcesso(cpf);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}finally{
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return 0;
	}

	@Override
	public Acesso procuraAcessoCEB(String cpf) {
		try{
			setDao(FactoryDAO.getDaoAcesso(FactoryConnection.getInstance().criaConexao()));
			return getDao().procuraAcessoCEB(cpf);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}finally{
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return null;
	}

	@Override
	public ArrayList<Acesso> consultaAcesso(String nome) {
		try{
			setDao(FactoryDAO.getDaoAcesso(FactoryConnection.getInstance().criaConexao()));
			return getDao().consultaAcesso(nome);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}finally{
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return null;
	}

	@Override
	public Acesso consultaAcessoCpf(String cpf) {
		try{
			setDao(FactoryDAO.getDaoAcesso(FactoryConnection.getInstance().criaConexao()));
			return getDao().consultaAcessoCpf(cpf);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}finally{
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return null;
	}

	@Override
	public int alteraAcesso(Acesso acesso) {
		try{
			setDao(FactoryDAO.getDaoAcesso(FactoryConnection.getInstance().criaConexao()));
			acesso.setSenha(EncriptaSenha.encripta(acesso.getSenha()));
			return getDao().alteraAcesso(acesso);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}finally{
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return 0;
	}

	@Override
	public boolean verificaPrimeiroAcesso() {
		try{
			setDao(FactoryDAO.getDaoAcesso(FactoryConnection.getInstance().criaConexao()));
			return getDao().verificaPrimeiroAcesso();
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}finally{
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return false;
	}

	@Override
	public void criaBanco(String[] sql) {
		try{
			setDao(FactoryDAO.getDaoAcesso(FactoryConnection.getInstance().criaConexao()));
			getDao().criaBanco(sql);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}finally{
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
	}
	
	public void primeiroAcesso() {
		try{
			setDao(FactoryDAO.getDaoAcesso(FactoryConnection.getInstance().criaConexao()));
			getDao().verificaPrimeiroAcesso();
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}finally{
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
	}
}
